import os
from flask import Blueprint, jsonify,request
from flask_cors import CORS
import json
from service.scenarioService import ScenarioService


scenario_controller = Blueprint('scenario_controller', __name__)
CORS(scenario_controller)

@scenario_controller.route('/get_scenario_list',methods=['POST'])
def getScenarioList()->None:
    return json.dumps([ob.__dict__ for ob in ScenarioService().getScenarioList()])

@scenario_controller.route('/get_scenario_by_id',methods=['POST'])
def getScenarioListById()->None:
    request_json = request.json
    id = request_json.get("id")
    return json.dumps(ScenarioService().getScenarioById(id).__dict__)

@scenario_controller.route('/add_scenario_item',methods=['POST'])
def addScenarioItem()->None:
    request_json = request.json
    scenario_id = request_json.get("scenario_id")
    new_scenario_item = request_json.get("new_scenario")
    ScenarioService().addScenarioItem(new_scenario_item,scenario_id)
    return []

@scenario_controller.route('/create_scenario',methods=['POST'])
def createScenario()->None:
    new_scenario = {}
    new_scenario['name'] = request.form['name']
    new_scenario['description'] = request.form['description']
    new_scenario['file'] = request.files['file']
    new_scenario['image'] = request.files['image']
    result = ScenarioService().createScenario(new_scenario)
    return []

# @scenario_controller.route('/ipload_image',methods=['POST'])
# def UploadImage()->None:
#     new_scenario['file'] = request.files['file']
#     result = ScenarioService().createScenario(new_scenario)
#     return []

@scenario_controller.route('/download/<filename>', methods=['GET'])
def downloadFile(filename):
    return ScenarioService().getFileByName(filename)


@scenario_controller.route('/image/<filename>', methods=['GET'])
def getIamgeByName(filename):
    return ScenarioService().getIamgeByName(filename)

